1 import tkinter as tk
2 import random
3
4 #Function to swap two bars that will be animated
5 def swap(pos_0, pos_1):
6     bar11, _, bar12, _ = canvas.coords(pos_0)
7     bar21, _, bar22, _ = canvas.coords(pos_1)
8     canvas.move(pos_0, bar21-bar11,
0)
9     canvas.move(pos_1, bar12-bar22,
0)
10
11 worker = None
12
13 #Insertion Sort
14 def _insertion_sort():
15     
global barList
16     
global lengthList
17
18     
for i in range(len(lengthList)):
19         cursor = lengthList[i]
20         cursorBar = barList[i]
21         pos = i
22
23         
while pos > 0 and lengthList[pos - 1] > cursor:
24             lengthList[pos] = lengthList[pos -
1]
25             barList[pos], barList[pos -
1] = barList[pos - 1], barList[pos]
26             swap(barList[pos],barList[pos-
1])
27             
yield
28             pos -=
1
29
30         lengthList[pos] = cursor
31         barList[pos] = cursorBar
32         swap(barList[pos],cursorBar)
33
34
35 #Bubble Sort
36 def _bubble_sort():
37     
global barList
38     
global lengthList
39     
40     
for i in range(len(lengthList) - 1):
41         
for j in range(len(lengthList) - i - 1):
42             
if(lengthList[j] > lengthList[j + 1]):
43                 lengthList[j] , lengthList[j +
1] = lengthList[j + 1] , lengthList[j]
44                 barList[j], barList[j +
1] = barList[j + 1] , barList[j]
45                 swap(barList[j +
1] , barList[j])
46                 
yield
47            
48
49 #Selection Sort
50 def _selection_sort():
51     
global barList
52     
global lengthList
53
54     
for i in range(len(lengthList)):
55         min = i
56         
for j in range(i + 1 ,len(lengthList)):
57             
if(lengthList[j] < lengthList[min]):
58                 min = j
59         lengthList[min], lengthList[i] = lengthList[i] ,lengthList[min]
60         barList[min] , barList[i] = barList[i] , barList[min]
61         swap(barList[min] , barList[i])
62         
yield
63
64
65 #Triggering Fuctions
66
67 def insertion_sort():
68     
global worker
69     worker = _insertion_sort()
70     animate()
71
72 def selection_sort():
73     
global worker
74     worker = _selection_sort()
75     animate()
76
77 def bubble_sort():
78     
global worker
79     worker = _bubble_sort()
80     animate()
81
82
83
84 #Animation Function
85 def animate():
86     
global worker
87     
if worker is not None:
88         
try:
89             next(worker)
90             window.after(
10, animate)
91         except StopIteration:
92             worker = None
93         
finally:
94             window.after_cancel(animate)
95
96
97 #Generator function
for generating data
98 def generate():
99     
global barList
100     
global lengthList
101     canvas.delete(
'all')
102     barstart =
5
103     barend =
15
104     barList = []
105     lengthList = []
106
107     #Creating a rectangle
108     
for bar in range(1, 60):
109         randomY = random.randint(
1, 360)
110         bar = canvas.create_rectangle(barstart, randomY, barend,
365, fill='yellow')
111         barList.append(bar)
112         barstart +=
10
113         barend +=
10
114
115     #Getting length of the bar and appending
into length list
116     
for bar in barList:
117         bar = canvas.coords(bar)
118         length = bar[
3] - bar[1]
119         lengthList.append(length)
120
121     #Maximum
is colored Red
122     #Minimum
is colored Black
123     
for i in range(len(lengthList)-1):
124         
if lengthList[i] == min(lengthList):
125             canvas.itemconfig(barList[i], fill=
'red')
126         elif lengthList[i] == max(lengthList):
127             canvas.itemconfig(barList[i], fill=
'black')
128
129
130
131 #Making a window
using the Tk widget
132 window = tk.Tk()
133 window.title(
'Sorting Visualizer')
134 window.geometry(
'600x450')
135
136 #Making a Canvas within the window to display contents
137 canvas = tk.Canvas(window, width=
'600', height='400')
138 canvas.grid(column=
0,row=0, columnspan = 50)
139
140 #Buttons
141 insert = tk.Button(window, text=
'Insertion Sort', command=insertion_sort)
142 select
= tk.Button(window, text='Selection Sort', command=selection_sort)
143 bubble = tk.Button(window, text=
'Bubble Sort', command=bubble_sort)
144 shuf = tk.Button(window, text=
'Shuffle', command=generate)
145 insert.grid(column=
1,row=1)
146 select
.grid(column=2,row=1)
147 bubble.grid(column=
3,row=1)
148 shuf.grid(column=
0, row=1)
149
150 generate()
151 window.mainloop()


Gõ tìm kiếm nhanh...